package it.hopeless.jee6.security;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * User: frico
 * Date: Jan 31, 2010
 * Time: 2:29:57 AM
 */
@Stateless
public class AuthenticationService {
    private final Logger logger = LoggerFactory.getLogger(AuthenticationService.class);
    @PersistenceContext(name = "jee6")
    private EntityManager entityManager;

    public boolean authenticate(User user){
        logger.debug("Authenticating user through {}, {} from service",user.getUsername(),user.getPassword());
        User u = (User) entityManager.createQuery("select u from User u where u.username =:username and u.password =:password")
                .setParameter("username",user.getUsername())
                .setParameter("password",user.getPassword()).getSingleResult();
        return u!=null;
    }

    // used only for tests
    public void setEntityManager(EntityManager em) {
        this.entityManager = em;
    }
}
